package com.example.mall.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.Mapper.BrandsMapper;
import com.example.mall.entity.Brands;
import com.example.mall.param.BasePageParam;
import com.example.mall.param.brands.AddBrandsParam;
import com.example.mall.param.brands.BrandsNameParam;
import com.example.mall.param.brands.EditBrandsParam;
import com.example.mall.param.brands.FindBrandsParam;
import com.example.mall.service.BrandsService;
import com.example.mall.util.exception.BusinessException;
import com.example.mall.vo.TypeByProductVo;
import com.example.mall.vo.brands.FindAllVo;
import com.example.mall.vo.brands.FindByIdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class BrandsServiceImpl extends ServiceImpl<BrandsMapper, Brands> implements BrandsService {

    @Autowired
    private BrandsMapper brandsMapper;

    @Override
    public Integer edit(EditBrandsParam param) throws BusinessException{
        Brands brands = brandsMapper.selectById(param.getId());

        if(param.getId() == null || param.getId() == 0 || brands == null){
            throw new BusinessException("操作失败！");
        }

        if(param.getAbbreviation() != null ){
            brands.setAbbreviation(param.getAbbreviation());
        }

        if(param.getBrandsAddress() != null){
            brands.setBrandsAddress(param.getBrandsAddress());
        }

        if(param.getBrandsName() != null){
            brands.setBrandsName(param.getBrandsName());
        }

        if(param.getStatus() != null){
            brands.setStatus(param.getStatus());
        }

        return brandsMapper.updateById(brands);
    }

    @Override
    public Integer add(AddBrandsParam param) {
        Brands brands = new Brands();

        if(param.getAbbreviation() != null ){
            throw new BusinessException("操作失败！");
        }
        if(param.getBrandsAddress() != null){
            throw new BusinessException("操作失败！");
        }
        if(param.getBrandsName() != null){
            throw new BusinessException("操作失败！");
        }
        if(param.getStatus() != null){
            throw new BusinessException("操作失败！");
        }

        brands.setStatus(param.getStatus());
        brands.setBrandsName(param.getBrandsName());
        brands.setBrandsAddress(param.getBrandsAddress());
        brands.setAbbreviation(param.getAbbreviation());
        brands.setCreateTime(new Date());
        return brandsMapper.insert(brands);
    }

    @Override
    public FindByIdVo BrandsFindById(FindBrandsParam param) {
        Brands brands = brandsMapper.selectById(param.getId());
        FindByIdVo vo = new FindByIdVo();
        vo.setAbbreviation(brands.getAbbreviation());
        vo.setBrandsAddress(brands.getBrandsAddress());
        vo.setBrandsName(brands.getBrandsName());
        vo.setId(brands.getId());
        return vo;
    }

    @Override
    public HashMap BrandsFindAll(BasePageParam param) {
        Integer pageNo = param.getPageNo();
        Integer pageSize = param.getPageSize();
        IPage<TypeByProductVo> page = new Page<>(pageNo, pageSize);
        List vo =  brandsMapper.findAllBrands(page);
        HashMap<String , Object> map = new HashMap<>();
        map.put("data", vo);
        return map;
    }

    @Override
    public HashMap findByFlow(BasePageParam param) {
        Integer pageNo = param.getPageNo();
        Integer pageSize = param.getPageSize();
        IPage<TypeByProductVo> page = new Page<>(pageNo, pageSize);
        List vo =  brandsMapper.findByFlow(page);
        HashMap<String , Object> map = new HashMap<>();
        map.put("data", vo);
        return map;
    }

    @Override
    public HashMap findByBrandsName(BrandsNameParam param) {
        Integer pageNo = param.getPageNo();
        Integer pageSize = param.getPageSize();
        IPage<TypeByProductVo> page = new Page<>(pageNo, pageSize);
        List vo = brandsMapper.findByBrandsName(page,param.getName());
        HashMap<String , Object> map = new HashMap<>();
        map.put("data", vo);
        return map;
    }
}
